package com.teamwork.module03.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.teamwork.common.entity.module03.ArticleLikes;
import com.teamwork.module03.mapper.ArticleLikesMapper;
import com.teamwork.module03.service.ArticleLikeService;
import org.springframework.stereotype.Service;

@Service
public class ArticleLikeServiceImpl extends ServiceImpl<ArticleLikesMapper, ArticleLikes> implements ArticleLikeService {
    /**
     * 新增点赞记录
     * @param userid
     * @param artid
     */
    @Override
    public boolean InsertOneByUidAndAid(Integer userid, Integer artid) {

        //先查询是否点赞过
        LambdaQueryWrapper<ArticleLikes> wrapper =new LambdaQueryWrapper<>();
        wrapper.eq(ArticleLikes::getUserId,userid).eq(ArticleLikes::getArticleId,artid);

        ArticleLikes one = this.getOne(wrapper);
        if(one!=null){
            return false;
        }
        ArticleLikes articleLikes =new ArticleLikes();
        articleLikes.setUserId(userid);
        articleLikes.setArticleId(artid);

        return this.save(articleLikes);

    }

    /**
     * 取消点赞文章
     * @param userid
     * @param artid
     * @return
     */
    @Override
    public boolean deleteByUidAndAid(Integer userid, Integer artid) {

        LambdaQueryWrapper<ArticleLikes> wrapper =new LambdaQueryWrapper<>();
        wrapper.eq(ArticleLikes::getUserId,userid).eq(ArticleLikes::getArticleId,artid);
        return this.remove(wrapper);
    }

    /**
     * 获取点赞根据 用户id 文章id
     * @param userid 用户id
     * @param artid 文章id
      * @return
     */
    @Override
    public ArticleLikes getByUseridAndArtid(Integer userid, Integer artid) {
        LambdaQueryWrapper<ArticleLikes> wrapper =new LambdaQueryWrapper<>();
        wrapper.eq(ArticleLikes::getUserId,userid).eq(ArticleLikes::getArticleId,artid);

        return  this.getOne(wrapper);
    }
}
